home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / src / exampleCode / games / seahaven / main.c++ < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-02  |  4.9 KB  |  185 lines

  1. /*
  2.  * Copyright 1992, 1993, 1994, Silicon Graphics, Inc.
  3.  * All Rights Reserved.
  4.  *
  5.  * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
  6.  * the contents of this file may not be disclosed to third parties, copied or
  7.  * duplicated in any form, in whole or in part, without the prior written
  8.  * permission of Silicon Graphics, Inc.
  9.  *
  10.  * RESTRICTED RIGHTS LEGEND:
  11.  * Use, duplication or disclosure by the Government is subject to restrictions
  12.  * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
  13.  * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
  14.  * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
  15.  * rights reserved under the Copyright Laws of the United States.
  16.  */
  17. /*
  18.  * Author:  Terry Weissman
  19.  *          weissman@sgi.com
  20.  */
  21.  
  22. #define MAIN
  23.  
  24.  
  25. #include "seahaven.h"
  26.  
  27. #include <stdio.h>
  28. #include <stdlib.h>
  29. #include <string.h>
  30. #include <X11/Xutil.h>
  31.  
  32.  
  33. void Punt(char *str) {
  34.     fprintf(stderr, "%s: %s\n", progname, str);
  35.     exit(1);
  36. }
  37.  
  38. static void Usage() {
  39.     fprintf(stderr, "Usage: %n [-d display] [-speedup speedfactor]\n",
  40.         progname);
  41.     exit(1);
  42. }
  43.  
  44.  
  45. main(int argc, char **argv) {
  46.     char *displayname = NULL;
  47.     Bool sync = False;
  48.     int i;
  49.  
  50.     progname = argv[0];
  51.  
  52.     speedup = 6;
  53.  
  54.     for (i=1 ; i<argc ; i++) {
  55.     if (i < argc - 1 && (strcmp(argv[i], "-d") == 0 ||
  56.                  strcmp(argv[i], "-display") == 0)) {
  57.         displayname = argv[++i];
  58.     } else if (strcmp(argv[i], "-sync") == 0) {
  59.         sync = True;
  60.     } else if (strcmp(argv[i], "-speedup") == 0) {
  61.         speedup = atoi(argv[++i]);
  62.         if (speedup <= 0) Usage();
  63.     } else {
  64.         Usage();
  65.     }
  66.     }
  67.  
  68.     inautoplay = False;
  69.     score = NULL;
  70.  
  71.     dpy = XOpenDisplay(displayname);
  72.     if (!dpy) {
  73.     Punt("Can't open display.");
  74.     }
  75.     screen = DefaultScreen(dpy);
  76.     cmap = DefaultColormap(dpy, screen);
  77.     XSynchronize(dpy, sync);
  78.  
  79.     Visual *visual = DefaultVisual(dpy, screen);
  80.  
  81.     hascolor = (visual->c_class != StaticGray &&
  82.         visual->c_class != GrayScale && visual->bits_per_rgb > 1);
  83.     hasgrey = (
  84.     (visual->c_class == StaticGray || visual->c_class == GrayScale)
  85.     && visual->bits_per_rgb > 1);
  86.  
  87.     backpixel = GetColor("forestgreen", "grey80", WhitePixel(dpy, screen));
  88.  
  89.     XSetWindowAttributes attributes;
  90.     long valuemask = CWEventMask | CWBackPixel;
  91.     attributes.event_mask = KeyPressMask;
  92.     attributes.background_pixel = backpixel;
  93.  
  94.     toplevel = XCreateWindow(dpy, DefaultRootWindow(dpy), 0, 0,
  95.                  GAMEWIDTH, GAMEHEIGHT, 1, (int) CopyFromParent,
  96.                  InputOutput, (Visual *) CopyFromParent,
  97.                  valuemask, &attributes);
  98.     XSetIconName(dpy, toplevel, "Seahaven");
  99.     XStoreName(dpy, toplevel, "Seahaven Towers");
  100.  
  101.     font = XLoadQueryFont
  102.     (dpy, "-*-helvetica-medium-r-normal--*-120-*-*-p-*-iso8859-1");
  103.     if (font == NULL) font = XLoadQueryFont(dpy, "fixed");
  104.     if (font == NULL) Punt("Can't find a font!");
  105.  
  106.     CardInit();
  107.     StackInit();
  108.  
  109.     score = new ScoreRec();
  110.  
  111.     if (score->getNeedStartingGame()) {
  112.     score->wonGame();
  113.     NewGame();
  114.     }
  115.  
  116.     int y = GAMEHEIGHT - font->ascent - font->descent - 10;
  117.     int width, width2, width3;
  118.     Window undobutton = CreateButtonWindow("Undo (U)", 10, y,
  119.                        SouthWestGravity, &width);
  120.     Window redobutton = CreateButtonWindow("Redo (R)", 10 + width + 5, y,
  121.                        SouthWestGravity, &width2);
  122.     Window autobutton = CreateButtonWindow("Autoplay",
  123.                            10 + width + 5 + width2 + 5, y,
  124.                        SouthWestGravity, NULL);
  125.     Window quitbutton = CreateButtonWindow("Quit", 0, 0,
  126.                        SouthEastGravity, &width);
  127.     Window newgamebutton = CreateButtonWindow("New Game", 0, 0,
  128.                           SouthEastGravity, &width2);
  129.     Window restartbutton = CreateButtonWindow("Restart", 0, 0,
  130.                           SouthEastGravity, &width3);
  131.     XMoveWindow(dpy, quitbutton, GAMEWIDTH - width - 10, y);
  132.     XMoveWindow(dpy, newgamebutton, GAMEWIDTH - width - width2 - 20, y);
  133.     XMoveWindow(dpy, restartbutton,
  134.         GAMEWIDTH - width - width2 - width3 - 30, y);
  135.  
  136.     XMapWindow(dpy, toplevel);
  137.  
  138.     for (;;) {
  139.     XEvent event;
  140.     GetInterestingEvent(&event);
  141.     if (!CardHandleEvent(&event)) {
  142.         if (event.type == KeyPress) {
  143.         char buf[100];
  144.         int length;
  145.         length = XLookupString((XKeyEvent *)&event, buf, 100, NULL,
  146.                        NULL);
  147.         for (i=0 ; i<length ; i++) {
  148.             switch(buf[i]) {
  149.               case 'u':
  150.               case 'U':
  151.               case 'z':
  152.               case 'Z':
  153.             DoUndo();
  154.             break;
  155.               case 'r':
  156.               case 'R':
  157.             DoRedo();
  158.             break;
  159.               case '\016':
  160.             NewGame();
  161.             break;
  162.               case '\003':
  163.             XUnmapWindow(dpy, toplevel);
  164.             XFlush(dpy);
  165.             exit(0);
  166.             break;
  167.             }
  168.         }
  169.         } else if (event.type == ButtonPress) {
  170.         Window w = event.xbutton.window;
  171.         if (w == undobutton) DoUndo();
  172.         else if (w == redobutton) DoRedo();
  173.         else if (w == restartbutton) DoRestart();
  174.         else if (w == autobutton) DoAutoPlay();
  175.         else if (w == newgamebutton) NewGame();
  176.         else if (w == quitbutton) {
  177.             XUnmapWindow(dpy, toplevel);
  178.             XFlush(dpy);
  179.             exit(0);
  180.         }
  181.         }
  182.     }
  183.     }
  184. }
  185.